var config = require('../../../utils/config.js');
var netutil = require('../../../utils/netutil.js');
var baseutil = require('../../../utils/baseutil.js');
let { WeToast } = require('../../../src/wetoast.js');
var app = getApp()

const date = new Date()
const years = []
const months = []
const days = []
const daysX = []
const daysD = []
const daysP = []
const daysR = []
var mDay

const dateDialogType = 1;//出生日期

for (let i = 1900; i <= 2050; i++) {
  years.push(i)
}
for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
for (let i = 1; i <= 30; i++) {
  daysX.push(i)
}
for (let i = 1; i <= 31; i++) {
  daysD.push(i)
}
for (let i = 1; i <= 28; i++) {
  daysP.push(i)
}
for (let i = 1; i <= 29; i++) {
  daysR.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagetype: 1,//0编辑 1新增
    editPassenger: null,

    //选择日期
    years: years,
    months: months,
    days: days,


    CName: null,//中文名
    Surname: null,//英文姓
    Givenname: null,//英文名
    isAdult: 1,//1成人 2儿童
    selectCardTypeText: "身份证",//证件类型文本
    selectCardType: 0,//证件类型
    CardNo: null,//证件号码
    CardValidTime: null,//证件有效期
    Country: null,//国籍
    selectNation: null,
    Sex: -1,//性别 性别[1-男,2-女]
    BirthDay: null,//生日
    Mobie: null,//手机号码
    Pid: null,//常旅客id

    isShowCardDialog: false,//是否显示证件选择dialog
    isShowDateDialog: false,//是否显示日期选择dialog
    dateDialogType: 0,//0表示出生日期  1表示证件有效期
    isShowNationDialog: false,//是否显示国籍选择的dialog

    tempselectNation: null,
    tempCountry: null,

    isFrom: 0,//1表示来自我的-常旅客列表  0表示来自订单-常旅客
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pagetype = options.type;

    var editPassenger = app.globalData.tempData

    wx.setNavigationBarTitle({ title: editPassenger == null ? "新增乘客" : "编辑乘客" });

    //创建可重复使用的toast实例，并附加到this上，通过this.wetoast访问
    new app.WeToast()


    var myDate = new Date();


    if (editPassenger != null) {
      app.globalData.tempData = null
      var Ename = editPassenger.EName;
      baseutil.log(Ename)

      if (Ename == null) {
        editPassenger.Surname = ''
        editPassenger.Givenname = ''
      } else {
        editPassenger.Surname = Ename.split('/')[0]
        editPassenger.Givenname = Ename.split('/')[1]
      }
      if (editPassenger.BirthDay == null) {
        editPassenger.isAdult = 1
      } else {
        var age = baseutil.getAgeBybirth(editPassenger.BirthDay.split(" ")[0])
        editPassenger.isAdult = (age >= 12 ? 1 : 2)
        baseutil.log(age + "=age")
      }

      //身份证 = 0,护照 = 1,港澳通行证 = 2,台胞证 = 3,台湾通行证 = 4,回乡证 = 5,军官证 = 6,其他 = 10]

      var selectCardTypeText = null;
      switch (editPassenger.CardType) {
        case 0:
          selectCardTypeText = "身份证"
          break;
        case 1:
          selectCardTypeText = "护照"
          break;
        case 2:
          selectCardTypeText = "港澳通行证"
          break;
        case 3:
          selectCardTypeText = "台胞证"
          break;
        case 4:
          selectCardTypeText = "台湾通行证"
          break;
        case 5:
          selectCardTypeText = "回乡证"
          break;
        case 6:
          selectCardTypeText = "军官证"
          break;
        case 10:
          selectCardTypeText = "其他"
          break;

      }
      var selectNation = new Object();
      var CountryData = editPassenger.Country.split("(");
      selectNation.ChineseName = CountryData[0]
      if (CountryData[1] == null) {
        selectNation.ShortName = CountryData[1]

      } else {
        selectNation.ShortName = CountryData[1].replace(")", "")

      }

      this.setData({
        CName: editPassenger.CName,
        Surname: editPassenger.Surname,
        Givenname: editPassenger.Givenname,
        isAdult: editPassenger.isAdult,
        selectCardTypeText: selectCardTypeText,
        selectCardType: editPassenger.CardType,
        CardNo: editPassenger.CardNo,
        CardValidTime: editPassenger.CardValidTime.split(" ")[0],
        Country: selectNation.ChineseName,
        selectNation: selectNation,
        Sex: editPassenger.Sex,
        BirthDay: editPassenger.BirthDay,
        Mobie: editPassenger.Mobie,
        Pid: editPassenger.Pid,
        isFrom: options.isFrom
      })
      new app.WeToast()
    } else {
      var selectNation = new Object();
      selectNation.ChineseName = "中国大陆"
      selectNation.ShortName = "CN"
      this.data.tempselectNation = selectNation;
      this.data.tempCountry = selectNation.ChineseName

      this.data.tempBirthDay = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();

      this.data.tempCardValidTime = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();

    }

    var year = parseInt(date.getFullYear()) - 1900


    this.setData({
      pagetype: pagetype,
      editPassenger: editPassenger,
      value: [year, date.getMonth(), date.getDate() - 1],
      isFrom: options.isFrom,
    })
    this.getNationData();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 选择出生日期
   */
  bindChange: function (e) {
    const val = e.detail.value
    var mdays
    let year = this.data.years[val[0]]
    let month = this.data.months[val[1]]
    let day = this.data.days[val[2]]
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
      mdays = daysR
    } else {
      mdays = daysP
    }
    if (month == 4 || month == 6 || month == 9 || month == 11) {
      mdays = daysX
    } else if (month == 1 || month == 3 || month == 5 || month == 7 | month == 8 | month == 10 | month == 12) {
      mdays = daysD
    }

    if (this.data.dateDialogType == 0) {
      this.setData({
        days: mdays,
        tempBirthDay: year + "/" + (month >= 10 ? "" : "0") + month + "/" + (day >= 10 ? "" : "0") + day,
      })
    } else {
      this.setData({
        days: mdays,
        tempCardValidTime: year + "/" + (month >= 10 ? "" : "0") + month + "/" + (day >= 10 ? "" : "0") + day,
      })
    }

  },
  getCName(e) {
    this.data.CName = e.detail.value
  },
  getSurname(e) {
    this.data.Surname = e.detail.value
  },
  getGivenname(e) {
    this.data.Givenname = e.detail.value
  },
  getCardNo(e) {
    this.data.CardNo = e.detail.value
  },
  getMobie(e) {
    this.data.Mobie = e.detail.value
  },
  changeCardType() {
    this.setData({ isShowCardDialog: true })
  },
  selectCard(e) {
    var cardtype = e.currentTarget.dataset.cardtype

    var selectCardTypeText = null;
    var CardNo = null;//证件号码
    var CardValidTime = null;//证件有效期

    var editPassenger = this.data.editPassenger

    switch (cardtype) {
      case 0:
        selectCardTypeText = "身份证"
        if (this.data.editPassenger == 0) {//编辑
          CardNo = editPassenger.IDCard;
          // CardValidTime=
        }
        break;
      case 1:
        selectCardTypeText = "护照"
        break;
      case 2:
        selectCardTypeText = "港澳通行证"
        break;
      case 3:
        selectCardTypeText = "台胞证"
        break;
      case 4:
        selectCardTypeText = "台湾通行证"
        break;
      case 5:
        selectCardTypeText = "回乡证"
        break;
      case 6:
        selectCardTypeText = "军官证"
        break;
      case 10:
        selectCardTypeText = "其他"
        break;

    }
    this.setData({
      isShowCardDialog: false,
      selectCardTypeText: selectCardTypeText,
      selectCardType: cardtype
    })
  },
  showDateDialog(e) {
    var type = e.currentTarget.dataset.type//0表示出生日期  1表示证件有效期
    this.setData({
      isShowDateDialog: true,
      dateDialogType: type,//0表示出生日期  1表示证件有效期
    })
  },
  cancelDateSelect() {
    this.setData({
      isShowDateDialog: false
    })
  }, confirmDateSelect() {
    if (this.data.dateDialogType == 0) {
      this.setData({
        isShowDateDialog: false,
        BirthDay: this.data.tempBirthDay,
      })
    } else {
      this.setData({
        isShowDateDialog: false,
        CardValidTime: this.data.tempCardValidTime,
      })
    }

  },
  selectSex(e) {
    var sex = e.currentTarget.dataset.sex//性别[1-男,2-女]
    this.setData({
      Sex: sex
    })
  },
  selectIsAdult(e) {
    var isadult = e.currentTarget.dataset.isadult//1成人 2儿童
    baseutil.log(isadult)
    this.setData({
      isAdult: isadult
    })
  },
  getNationData() {
    var THIS = this
    var url = config.getBaseUrl("IFlightApi", "GetIFlightCountry");
    baseutil.log(url);
    netutil.post(
      url, "",
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var NationList = res.data.Data.All

          THIS.setData({
            NationList: NationList
          })
        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      })
  },
  showNationDialog() {
    this.setData({
      isShowNationDialog: true,
    })
  },
  confirmNationSelect() {
    this.setData({
      isShowNationDialog: false,
      selectNation: this.data.tempselectNation,
      Country: this.data.tempCountry
    })
  },
  cancelNationSelect() {
    this.setData({
      isShowNationDialog: false,
    })
  },
  bindNationChange(e) {
    const val = e.detail.value
    let tempselectNation = this.data.NationList[val[0]]
    let tempCountry = tempselectNation.ChineseName
    baseutil.log(tempCountry)
    this.setData({
      tempselectNation: tempselectNation,
      tempCountry: tempCountry
    })
  },
  savePassagerData() {

    // CName: null,//中文名
    // Surname: null,//英文姓
    // Givenname: null,//英文名
    // isAdult: 1,//1成人 2儿童
    // selectCardTypeText: "身份证",//证件类型文本
    // selectCardType: 0,//证件类型
    // CardNo: null,//证件号码
    // CardValidTime: null,//证件有效期
    // Country: null,//国籍
    // selectNation: null,
    // Sex: -1,//性别 性别[1-男,2-女]
    // BirthDay: null,//生日
    // Mobie: null,//手机号码
    // Pid: null,//常旅客id

    var THIS = this
    var url = config.getBaseUrl("CommonApi", "AddUpdateFrequentPassenger");
    baseutil.log(url);

    var Data = new Object();
    Data.UserID = app.globalData.TUserInfo.UserID
    if (this.data.Pid != null) {
      Data.Pid = this.data.Pid
    }
    if (this.data.CName == null || this.data.CName.length == 0) {
      this.wetoast.toast({
        title: '中文名不能为空!',
        duration: 1000
      })
      return;
    }
    Data.CName = this.data.CName
    if (this.data.isFrom == 1) {
      if (this.data.Surname == null || this.data.Surname.length == 0) {
        this.wetoast.toast({
          title: '英文姓不能为空!',
          duration: 1000
        })
        return;
      }
      Data.ELastName = this.data.Surname
    }


    if (this.data.isFrom == 1) {
      if (this.data.Givenname == null || this.data.Givenname.length == 0) {
        this.wetoast.toast({
          title: '英文名不能为空!',
          duration: 1000
        })
        return;
      }
      Data.EName = this.data.Givenname
    }

    if (this.data.selectCardType == null || this.data.selectCardType.length == 0) {
      this.wetoast.toast({
        title: '请选择证件类型!',
        duration: 1000
      })
      return;
    }
    Data.CardType = this.data.selectCardType

    if (this.data.CardNo == null || this.data.CardNo.length == 0) {
      this.wetoast.toast({
        title: '请填写证件号码!',
        duration: 1000
      })
      return;
    }
    Data.CardNo = this.data.CardNo
    baseutil.log(this.data.CardNo + "=CardNo")

    if (this.data.CardValidTime == null || this.data.CardValidTime.length == 0) {
      this.wetoast.toast({
        title: '请选择证件有效期!',
        duration: 1000
      })
      return;
    }
    Data.CardValidDate = this.data.CardValidTime
    if (this.data.isFrom == 1) {
      if (this.data.selectNation == null || this.data.selectNation.length == 0) {
        this.wetoast.toast({
          title: '请选择国籍!',
          duration: 1000
        })
        return;
      }
      Data.Country = this.data.selectNation.ChineseName + "(" + this.data.selectNation.ShortName + ")"
    }

    if (this.data.isFrom == 1) {
      if (this.data.Sex == -1) {
        this.wetoast.toast({
          title: '请选择性别!',
          duration: 1000
        })
        return;
      }
      Data.Sex = this.data.Sex
    }

    if (this.data.BirthDay == null || this.data.BirthDay.length == 0) {
      this.wetoast.toast({
        title: '请选择出生日期!',
        duration: 1000
      })
      return;
    }
    Data.BirthDay = this.data.BirthDay
    if (this.data.isFrom == 1) {
      if (this.data.Mobie == null || this.data.Mobie.length == 0) {
        this.wetoast.toast({
          title: '请填写手机号码!',
          duration: 1000
        })
        return;
      }
      Data.Mobie = this.data.Mobie
    }


    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var NationList = res.data.Data
          THIS.wetoast.toast({
            title: "保存成功!",
            duration: 1000
          })
          var pages = getCurrentPages();
          var prePage = pages[pages.length - 2];  //上一个页面
          prePage.getPassengerList();
          setTimeout(function () {
            baseutil.returnLastPage()
          }, 500)

        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      })
  },
  cancelSaveData() {
    baseutil.returnLastPage()
  },
  deletePassager() {
    var THIS = this
    var url = config.getBaseUrl("CommonApi", "DelFrequentPassenger");
    baseutil.log(url);

    var Data = new Object();
    Data.UserID = app.globalData.TUserInfo.UserID
    Data.PIds = this.data.Pid

    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var NationList = res.data.Data
          THIS.wetoast.toast({
            title: "删除成功!",
            duration: 1000
          })
          var pages = getCurrentPages();
          var prePage = pages[pages.length - 2];  //上一个页面
          prePage.getPassengerList();
          setTimeout(function () {
            baseutil.returnLastPage()
          }, 500)

        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      })
  },
})